# app.py
from flask import Flask, request, jsonify
from PIL import Image
from predictor import InsectPredictor
import io

app = Flask(__name__)
predictor = InsectPredictor("output/insect_resnet50.pdparams", "output/class_to_idx.json")

@app.route('/predict', methods=['POST'])
def predict():
    print(request.files)
    if 'file' not in request.files:
        return jsonify({'error': 'No file part'}), 400
    file = request.files['file']
    image = Image.open(file.stream).convert('RGB')
    label, confidence = predictor.predict(image)
    return jsonify({
        'label': label,
        'confidence': float(confidence)  # 转成 float 保证能序列化
    })

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=50000)

